//
//  LGDropDownMenu.m
//  UniverseProject
//
//  Created by king on 2019/8/20.
//  Copyright © 2019年 Cloud2209. All rights reserved.
//

#import "LGDropDownMenuView.h"

@interface LGDropDownMenuView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,weak) UITableView *tableView;
@property (nonatomic, weak) UIButton *headerBtn;
//选中的
@property (nonatomic, assign)NSIndexPath *hisSelIndexPath;

@end


@implementation LGDropDownMenuView

+ (instancetype)initWithOrginFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray{
    LGDropDownMenuView *menu = [[LGDropDownMenuView alloc] initWithFrame:frame];
    menu.dataArr = dataArray;
    menu.clipsToBounds = YES;
    [menu setupTab];
    [menu addLineWithFrame:CGRectMake(0, 43, K_SCREEN_WIDTH, 1) color:LGCOLOR(@"2b2b2b")];
    return menu;
}

/**
 设置表格
 */
- (void)setupTab{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, 220) style:UITableViewStylePlain];
    [self addSubview:tableView];
    
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    

    UIButton *headerBtn = [UIButton buttonWithTitle:self.dataArr[0] titleColor:LGCOLOR(@"ffffff") font:LGFONT(14) target:self action:@selector(menuShow)];
    headerBtn.frame = CGRectMake(0, 0, K_SCREEN_WIDTH, 44);
    [headerBtn setImage:LGIMAGE(@"trainangle") forState:UIControlStateNormal];
    [headerBtn jk_setImagePosition:LXMImagePositionRight spacing:5];
    [headerBtn setBackgroundColor:LGCOLOR(@"#24242D")];
    self.tableView.tableHeaderView = headerBtn;
    self.headerBtn = headerBtn;
}


- (void)setDataArr:(NSMutableArray *)dataArr{
    _dataArr =dataArr;
    
    self.hisSelIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.headerBtn setTitle:self.dataArr[0] forState:UIControlStateNormal];
    [self.headerBtn jk_setImagePosition:LXMImagePositionRight spacing:5];
}
     
     
     

#pragma mark -UITableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ScheduleVCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = LGCOLOR(@"#111111");
        cell.textLabel.text = self.dataArr[indexPath.row];
        cell.textLabel.font = LGFONT(14);
    }
    
    if (indexPath.row == self.hisSelIndexPath.row) {
        cell.textLabel.textColor = [UIColor orangeColor];
    }else{
        cell.textLabel.textColor = LGCOLOR(@"ffffff");
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hisSelIndexPath = indexPath;
    [self.tableView reloadData];
    [self menuOff];
    
    
    [self.headerBtn setTitle:self.dataArr[indexPath.row] forState:UIControlStateNormal];
    [self.headerBtn jk_setImagePosition:LXMImagePositionRight spacing:5];
    
    if (self.menuDidClickIndex) {
        self.menuDidClickIndex(indexPath.row);
    }
}
     
 - (void)menuShow{
     [self.superview bringSubviewToFront:self];
     [UIView animateWithDuration:0.5f animations:^{
         self.height =  (220.f > self.dataArr.count * 44) ? (self.dataArr.count + 1) * 44 : 220.f;
     }];
 }


- (void)menuOff{
    [UIView animateWithDuration:0.5f animations:^{
        self.height = 44.f;
    }];
}





@end
